package com.g2room.driving.grap.life;

import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.g2room.driving.grap.GenericGrap;
import com.g2room.driving.grap.life.model.po.ComCompany;
import com.g2room.myuall.grap.ky.model.po.GrapErr;
import com.g2room.myuall.grap.univ.po.UnivBeautyImgs;
import com.g2room.myuall.util.DbUtil;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;

public class GrapCqqqImg {
	// http://mycq.qq.com/forum.php?mod=collection&action=view&ctid=321&page=1
	public static void main(String[] args) throws Exception{
		
		List<String> grapUrls = new ArrayList<String>();
//		grapUrls.add("http://mycq.qq.com/forum.php?mod=collection&action=view&ctid=321&page=1");
//		grapUrls.add("http://mycq.qq.com/forum.php?mod=collection&action=view&ctid=321&page=2");
		grapUrls.add("http://mycq.qq.com/t-908208-1.htm");
//		grapUrls.add("http://mycq.qq.com/t-903016-1.htm");
//		grapUrls.add("http://mycq.qq.com/t-662252-1.htm");
		for(String url : grapUrls) {
			getDetail(url);
		}
    }
	
	/**
	 * 获取头部导航菜单
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public static void getList(String url) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(url);
		} catch (Exception e) {
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("1");
		}
		
		if(null == doc) return;
		Elements els = doc.select("#waterfall li div a ");
		if(null == els || els.isEmpty())  {
			System.out.println("获取出错");
			return ;
		}
		List<String> urls = new ArrayList<String>();
		for(Element el : els) {
			urls.add("http://mycq.qq.com/"+el.attr("href"));
		}
		
		for(String listUrl : urls) {
			getDetail(listUrl);
		}
	}
	

	public static void getDetail(String url) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(url);
		} catch(Exception e) {
			System.out.println(url);
			return;
		}
		List<String> imgs = new ArrayList<String>();
		Elements els = doc.select("#postlist ignore_js_op img");
		if(null != els && !els.isEmpty()) {
			for(Element el : els) {
				if(StringUtils.isNoneBlank(el.attr("file")))
				imgs.add(el.attr("file"));
			}
		}
//		List<ComCompany> dbccs = new ArrayList<ComCompany>();
//		for(ComCompany cc : ccs) {
//			getExist(cc, dbccs);
//		}
//		
		insert2ComDb(imgs);
	}
	

	public static void insert2ComDb(List<String> ubis) {
		String sql = "insert into life_beauty(img_url) values(?)";
		PreparedStatement ps = null;
		try {
			ps = DbUtil.getComConn().prepareStatement(sql);
			for(String cc : ubis) {
				ps.setString(1, cc);
				ps.addBatch();
			}
			ps.executeBatch();	
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}
